// Client

AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")

// Include

include('shared.lua')

// Initialize

function ENT:Initialize()
	self.Entity:SetModel("models/props_lab/jar01a.mdl")
	self.Entity:PhysicsInit(SOLID_VPHYSICS)
	self.Entity:SetMoveType(MOVETYPE_VPHYSICS)
	self.Entity:SetSolid(SOLID_VPHYSICS)
	self.Entity:SetUseType(ONOFF_USE)
	self.Entity:DrawShadow(false)
	self.Entity:SetNetworkedInt(0, 0)
end

// Key value

function ENT:KeyValue()
end

// Check owner

function ENT:CheckOwner(Entity)
	for K, V in pairs(g_SBoxObjects) do
		for B, J in pairs(V) do
			for _, E in pairs(J) do
				if (E == Entity) then
					return K
				end
			end
		end
	end
	
	// Let's try cameras
	
	for K, V in pairs(GAMEMODE.CameraList) do
		for B, J in pairs(V) do
			if (J == Entity) then
				return K
			end
		end
	end
	
	// Return nil

	return nil
end

// Think

function ENT:Think()
	local Width = self.Entity:GetNetworkedInt("Width")
	local Height = self.Entity:GetNetworkedInt("Height")
	local Owner = self.Entity:GetPlayer()
	local Unique = Owner:UniqueID()
	
	// Maximum
	
	local Minimum = self.Entity:GetPos()
	local Maximum = Vector(Minimum.x + Width, Minimum.y + Width, Minimum.z + Height)
	
	// Find in box
	
	local Entities = ents.FindInBox(Minimum, Maximum)
	
	// Loop through all found entities
	
	for K, V in pairs(Entities) do
		local Player = V:IsPlayer()
		
		if (Player) then
			if (V != Owner) then
				local Pos = self.Entity:GetPos() - V:GetPos() local Ang = Pos:Normalize()
				
				V:SetVelocity(Ang * -10000)
				
				// Sound
				
				V:EmitSound("weapons/fx/rics/ric4.wav")
			end
		else
			if (self:CheckOwner(V) != Unique) then
				local Phys = V:GetPhysicsObject()
				
				if (Phys) then
					local Valid = Phys:IsValid()
					
					if (Valid) then
						local Holding = V:IsPlayerHolding()
						
						if (Holding) then
							Phys:EnableMotion(false)
						else
							Phys:EnableMotion(true)
							
							// Velocity
							
							local Pos = self.Entity:GetPos() - V:GetPos() local Ang = Pos:Normalize()
							
							local Mass = Phys:GetMass() * 100
							
							Phys:SetVelocity(Ang * -Mass)
							
							// Sound
							
							V:EmitSound("weapons/fx/rics/ric4.wav")
						end
					end
				end
			end
		end
	end
end